<?php
class Arrays {
	/**
	 * print implode(Arrays::toString($array)
	 */
	public static function toString($array) {
		$result = array ();
		$depth = 0;
		foreach ( $array as $k => $v ) {
			$showval = (is_array ( $v ) ? "" : $v);
			// show the indents
			$result [] = str_repeat ( "  ", $depth );
			if ($depth == 0) {
				// this is a root node. no parents
				$result [] = "O ";
			} elseif (is_array ( $v )) {
				// this is a normal node. parents and children
				$result [] = "+ ";
			} else {
				// this is a leaf node. no children
				$result [] = "- ";
			}
			// show the actual node
			if ($showval == "") {
				$result [] = "<strong>{$k}</strong>:";
			} else {
				$result [] = $k . " (" . $showval . ")" . "  ";
			}
			if (is_array ( $v )) {
				// this is what makes it recursive, rerun for childs
				$temp = self::toTree ( $v, ($depth + 1) );
				foreach ( $temp as $t ) {
					$result [] = $t;
				}
			}
		}
		return implode ( $result );
	}
	private static function showType($showval) {
		// convert bools to text and quote 'text bools'!
		if (is_string ( $showval ) && ($showval == "true" || $showval == "false")) {
			return "\"{$showval}\"";
		} elseif (is_bool ( $showval ) && $showval === true) {
			return "true";
		} elseif (is_bool ( $showval ) && $showval === false) {			
			return "false";
		} elseif (is_null ( $showval )) {			
			return "null";
		} else {
			return $showval;
		}
	}
	private static function toTree($pieces, $depth = 0) {
		foreach ( $pieces as $k => $v ) {
			// skip the baseval thingy. Not a real node.
			// if($k == "__base_val") continue;
			// determine the real value of this node.
			$showval = (is_array ( $v ) ? "" : $v);
			$showval = self::showType ( $showval );
			// show the indents
			$result [] = str_repeat ( "  ", $depth );
			if ($depth == 0) {
				// this is a root node. no parents
				$result [] = "O ";
			} elseif (is_array ( $v )) {
				// this is a normal node. parents and children
				$result [] = "+ ";
			} else {
				// this is a leaf node. no children
				$result [] = "- ";
			}
			// show the actual node			
			if ($showval == "") {
				$result [] = "<strong>{$k}</strong>:";
			} else {
				$result [] = $k . ": <i>{$showval}</i>";
			}
			if (is_array ( $v )) {
				// this is what makes it recursive, rerun for childs
				$temp = self::toTree ( $v, ($depth + 1) );
				if (is_array ( $temp )) {
					foreach ( $temp as $t ) {
						$result [] = $t;
					}
				} else {
					$result [] = $t;
				}
			}
		}
		return $result;
	}
}
?>